/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package memoryManagement;

/**
 *
 * @author feiyun
 */
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Stack;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author xyc
 */
public class displayPanel extends JPanel {

    private Stack buff = new Stack();//存储从mem内取出的program利用的内存信息
    public panel1 p = new panel1(buff);//新建panel
    int sum;//判断内存的利用情况，并以此决定内存颜色

    public displayPanel(mem a) {
        program c = a.pList.lLink;
        //获取程序链利用的内存的信息
        sum = 0;//初始化sum
        while (c != null) {
            sum += c.ownMem.size;
            buff.push(c.ownMem.fPage);
            buff.push(c.ownMem.size);
            c = c.lLink;

        }

        //画图
        add(p);
    }

    //重画函数
    public void rePaint(mem a) {
        p.rePaint(a);

    }

    //void setTitle(String s) {
    //    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    //}
    class panel1 extends JPanel {

        Stack buff;//存储发送过来的内存利用信息

        public panel1(Stack buff) {
            this.buff = buff;
        }

        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);
            int i = 0, j = 0;
            g.drawRect(15, 55, 512, 100);

            Color r = new Color(20, 175, 48);
            Color m = new Color(222, 222, 18);
            Color b = new Color(222, 20, 10);
            Color L = null;//颜色的buffer

            if (sum > 384) {
                L = b;//忙时
            } else if (sum > 256) {
                L = m;//正常
            } else {
                L = r;//闲时
            }

            while (!this.buff.empty()) {
                i = (int) this.buff.pop();//i为这个buddy的大小
                j = (int) this.buff.pop();//j为这个buddy的起始位置

                g.setColor(L);
                g.fillRect((j - 1) * 4 +15, 55, i , 100);//
                g.setColor(new Color(0, 0, 0));
                g.drawLine((j - 1) * 4 +15, 55, (j - 1) * 4 +15 , 155);//画出每个buddy的前端
                g.drawLine((j - 1) * 4 +15 + i , 55, (j - 1) * 4  + i+15 , 155);//后端
                g.setFont(new Font("Tahoma", Font.BOLD, 16));
                g.drawString(i + "b", (j - 1) * 4 +25, 177);//string的位置

            }

        }

        //重画函数
        public void rePaint(mem a) {
            program c = a.pList.lLink;
            sum = 0;//初始化sum
            while (c != null) {
                sum += c.ownMem.size;
                buff.push(c.ownMem.fPage);
                buff.push(c.ownMem.size);
                c = c.lLink;
            }
            repaint();
        }
    }
}
